package com.augurit.wf.client.agency.convert;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.augurit.common.util.lang.DateUtils;
import com.augurit.wf.client.agency.web.form.WfAgencyForm;
import com.augurit.wf.common.entity.WfAgency;

public class WfAgencyConvertor {
	public static WfAgencyForm convertVoToForm(WfAgency entity) {
		if(entity != null) {
			WfAgencyForm form = new WfAgencyForm();
			form.setId(entity.getId());
			form.setPrincipal(entity.getPrincipal());
			form.setPrincipalName(entity.getPrincipalName());
			form.setAgent(entity.getAgent());
			form.setAgentName(entity.getAgentName());
			form.setAgentMode(entity.getAgentMode());
			form.setStartDate(entity.getStartDate());
			form.setEndDate(entity.getEndDate());
			form.setReason(entity.getReason());
			return form;
		}else
			return null;
	}
	
	public static void convertFormToVo(WfAgencyForm form, WfAgency entity) {
		if(entity != null && form != null) {
			entity.setId(form.getId());
			if(form.getPrincipal() != null && form.getPrincipal().trim().length() > 0)
				entity.setPrincipal(form.getPrincipal().trim());
			if(form.getPrincipalName() != null && form.getPrincipalName().trim().length() > 0)
				entity.setPrincipalName(form.getPrincipalName().trim());
			if(form.getAgent() != null && form.getAgent().trim().length() > 0)
				entity.setAgent(form.getAgent().trim());
			if(form.getAgentName() != null && form.getAgentName().trim().length() > 0)
				entity.setAgentName(form.getAgentName().trim());
			if(form.getAgentMode() != null && form.getAgentMode().trim().length() > 0)
				entity.setAgentMode(form.getAgentMode().trim());
			entity.setStartDate(form.getStartDate());
			entity.setEndDate(form.getEndDate());
			if(form.getReason() != null && form.getReason().trim().length() > 0)
				entity.setReason(form.getReason().trim());
		}
	}
	
	public static List<WfAgencyForm> convertVoListToFormList(List<WfAgency> wfAgencyList) {
		if(wfAgencyList != null && wfAgencyList.size() > 0) {
			List<WfAgencyForm> wfAgencyFormList = new ArrayList<WfAgencyForm>();
			for(int i=0; i<wfAgencyList.size(); i++) {
				wfAgencyFormList.add(convertVoToForm(wfAgencyList.get(i)));
			}
			return wfAgencyFormList;
		}
		return null;
	}
	
	public static List<Map> convertVoListToMapList(List<WfAgency> wfAgencyList) {
		if(wfAgencyList != null && wfAgencyList.size() > 0) {
			List<Map> mapList = new ArrayList<Map>();
			for(int i=0; i<wfAgencyList.size(); i++) {
				WfAgency entity = wfAgencyList.get(i);
				Map<String, String> map = new HashMap<String, String>();

				map.put("id", entity.getId() == null ? "" : entity.getId().toString());
				map.put("principal", entity.getPrincipal());
				map.put("principalName", entity.getPrincipalName());
				map.put("agent", entity.getAgent());
				map.put("agentName", entity.getAgentName());
				map.put("agentMode", entity.getAgentMode());
				map.put("startDate", DateUtils.dateTimeToString(entity.getStartDate(), "yyyy-MM-dd"));
				map.put("endDate", DateUtils.dateTimeToString(entity.getEndDate(), "yyyy-MM-dd"));
				map.put("reason", entity.getReason());
				
				mapList.add(map);
			}
			return mapList;
		}
		return null;
	}
	
	public static List<WfAgency> convertFormListToVoList(List<WfAgencyForm> wfAgencyFormList) {
		if(wfAgencyFormList != null && wfAgencyFormList.size() > 0) {
			List<WfAgency> wfAgencyList = new ArrayList<WfAgency>();
			for(int i=0; i<wfAgencyFormList.size(); i++) {
				WfAgency wfAgency = new WfAgency();
				convertFormToVo(wfAgencyFormList.get(i), wfAgency);
				wfAgencyList.add(wfAgency);
			}
			return wfAgencyList;
		}
		return null;
	}
}